/*
 * Copyright 2020-2021 redragon-erp/赤龙ERP
 * Author redragon/dongbin
 * This file is part of redragon-erp/赤龙ERP.

 * 赤龙ERP官方声明：
 * (1)赤龙ERP的知识产权归软件著作权人所有
 * (2)未经著作权人授权，不得将赤龙ERP用于任何形式的商业用途
 * (3)对侵权的组织或个人，著作权人将保留诉诸法律的权利

 * 赤龙ERP官网详见 <https://www.redragon-erp.com>
 * 赤龙ERP仓库详见 <https://gitee.com/redragon/redragon-erp> 
 * 如遇任何问题，请与著作权人联系
 */
package com.erp.oa.notice.service.spring;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.redragon.commons.lang.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Cache.CacheType;
import org.redragon.framework.annotation.Log;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.EhcacheUtil;
import org.redragon.framework.util.RedisUtil;
import org.redragon.framework.util.ShiroUtil;

import com.erp.hr.dao.model.HrStaffDepartmentR;
import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.hr.service.HrStaffDepartmentRService;
import com.erp.oa.notice.dao.SysNoticeRecordDao;
import com.erp.oa.notice.dao.model.SysNotice;
import com.erp.oa.notice.dao.model.SysNoticeRecord;
import com.erp.oa.notice.dao.model.SysNoticeRecordCO;
import com.erp.oa.notice.service.SysNoticeRecordService;
import com.erp.oa.notice.service.SysNoticeService;

@Service
@Transactional(rollbackFor=Exception.class)
public class SysNoticeRecordServiceImpl implements SysNoticeRecordService {

    //注入Dao
    @Autowired
    private SysNoticeRecordDao sysNoticeRecordDao;
    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private HrStaffDepartmentRService hrStaffDepartmentRService;
    @Autowired
    private HrCommonService hrCommonService;
    
    @Override
    public void insertDataObject(SysNoticeRecord obj) {
        this.sysNoticeRecordDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(SysNoticeRecord obj) {
        this.sysNoticeRecordDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(SysNoticeRecord obj) {
        this.sysNoticeRecordDao.insertOrUpdateDataObject(obj);
    }

    @Override
    public void deleteDataObject(SysNoticeRecord obj) {
        this.sysNoticeRecordDao.deleteDataObject(obj);
        //清空缓存
        this.clearCache(ShiroUtil.getPrincipal());
    }

    @Override
    public List<SysNoticeRecord> getDataObjects() {
        return this.sysNoticeRecordDao.getDataObjects();
    }

    @Override
    public SysNoticeRecord getDataObject(int id) {
        return this.sysNoticeRecordDao.getDataObject(id);
    }

    @Override
    public SysNoticeRecord getDataObject(String code) {
        return this.sysNoticeRecordDao.getDataObject(code);
    }

    @Override
    public List<SysNoticeRecord> getDataObjects(SysNoticeRecordCO paramObj) {
        return this.sysNoticeRecordDao.getDataObjects(paramObj);
    }

    @Override
    public List<SysNoticeRecord> getDataObjects(Pages pages) {
        return this.sysNoticeRecordDao.getDataObjects(pages);
    }
    
    @Override
    public List<SysNoticeRecord> getDataObjects(Pages pages, SysNoticeRecordCO paramObj) {
        return this.sysNoticeRecordDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, SysNoticeRecordCO paramObj) {
        return this.sysNoticeRecordDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<SysNoticeRecord> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, SysNoticeRecordCO paramObj) {
        return this.sysNoticeRecordDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public String publishNoticeRecord(Integer noticeId) {
        try {
            //要发布的职员set
            Set<String> staffCodeSet = new HashSet<String>();
            
            //获取通知信息
            SysNotice sysNotice = this.sysNoticeService.getDataObject(noticeId);
            //获取要发布的部门
            String[] departmentCodes = sysNotice.getPublishDepartment().split(",");
            for(String departmentCode: departmentCodes) {
                List<HrStaffDepartmentR> hrStaffDepartmentRList = this.hrStaffDepartmentRService.getStaffListByDepartment(departmentCode);
                for(HrStaffDepartmentR hrStaffDepartmentR: hrStaffDepartmentRList) {
                    staffCodeSet.add(hrStaffDepartmentR.getStaffCode());
                }
            }
            //获取要发布的职员
            if(StringUtils.isNotEmpty(sysNotice.getPublishPerson())){
                String[] personCodes=null;
                if(sysNotice.getPublishPerson().indexOf(",")>=0) {
                    personCodes = sysNotice.getPublishPerson().split(",");
                    for (String personCode : personCodes) {
                        staffCodeSet.add(personCode);
                    }
                }
                else{
                    staffCodeSet.add(sysNotice.getPublishPerson());
                }
            }
            
            //循环所有要发布的职员，插入通知记录
            for(String staffCodeTemp: staffCodeSet) {
                SysNoticeRecord sysNoticeRecord = new SysNoticeRecord();
                sysNoticeRecord.setRecordCode(SnowFlake.getConcurrentId().toString());
                sysNoticeRecord.setNoticeCode(sysNotice.getNoticeCode());
                sysNoticeRecord.setNoticeLevel(sysNotice.getNoticeLevel());
                sysNoticeRecord.setBusinessType(sysNotice.getBusinessType());
                sysNoticeRecord.setPublishPerson(staffCodeTemp);
                sysNoticeRecord.setTitle(sysNotice.getTitle());
                sysNoticeRecord.setContent(sysNotice.getContent());
                sysNoticeRecord.setCreatedBy(ShiroUtil.getPrincipal());
                sysNoticeRecord.setReadFlag("N");
                sysNoticeRecord.setStatus("Y");
                this.sysNoticeRecordDao.insertDataObject(sysNoticeRecord);
            }
            
            //更新发布状态
            sysNotice.setPublishFlag("Y");
            this.sysNoticeService.updateDataObject(sysNotice);
        }catch(Exception e) {
            return "通知发布失败";
        }
        
        return null;
    }
    
    @Override
    @Cache(cacheType=CacheType.ALL, cacheSeconds=600)
    public List<SysNoticeRecord> getSysNoticeRecordListByUsername(String username) {
        //获取职员信息
        HrStaffInfoRO hrStaffInfoRO = this.hrCommonService.getStaffInfo(username);
        
        //获取职员的通知列表
        Pages pages = new Pages();
        pages.setMax(10);
        pages.setPage(1);
        
        SysNoticeRecordCO sysNoticeRecordCO = new SysNoticeRecordCO();
        sysNoticeRecordCO.setPublishPerson(hrStaffInfoRO.getStaffCode());
        sysNoticeRecordCO.setReadFlag("N");
        List<SysNoticeRecord> sysNoticeRecordList = this.sysNoticeRecordDao.getDataObjects(pages, sysNoticeRecordCO);
        
        return sysNoticeRecordList;
    }
    
    @Override
    public void updateSysNoticeRecordForReadFlag(Integer recordId) {
        this.sysNoticeRecordDao.updateSysNoticeRecordForReadFlag(recordId);
        //清空缓存
        this.clearCache(ShiroUtil.getPrincipal());
    }

    @Override
    public List<SysNoticeRecord> getDataObjectsLimit(Pages pages, SysNoticeRecordCO paramObj) {
        return this.sysNoticeRecordDao.getDataObjectsLimit(pages,paramObj);
    }

    private void clearCache(String username) {
        //清除缓存
        EhcacheUtil.clearCacheBatch("*getSysNoticeRecordListByUsername*"+username.hashCode()+"*");
        RedisUtil.clearCacheBatch("*getSysNoticeRecordListByUsername*"+username.hashCode()+"*");
    }
    
}